import api from './api';

const quizGameService = {
  /**
   * 开始一局新游戏
   * @param {string} topic 游戏主题
   * @param {string} difficulty 难度
   * @returns {Promise<object>} 游戏会话数据
   */
  async startGame(topic, difficulty) {
    try {
      const response = await api.post('/api/quiz-game/start', { topic, difficulty });
      if (response.data.status === 0) {
        return response.data.data;
      } else {
        throw new Error(response.data.message || '开始游戏失败');
      }
    } catch (error) {
      const errorMessage = error.response?.data?.message || error.message;
      throw new Error(errorMessage);
    }
  },

  /**
   * 提交答案
   * @param {number} sessionId 会话ID
   * @param {number} questionIndex 当前问题索引
   * @param {string} answer 用户选择的答案
   * @returns {Promise<object>} 答案验证结果
   */
  async submitAnswer(sessionId, questionIndex, answer) {
    try {
      const response = await api.post('/api/quiz-game/submit', { sessionId, questionIndex, answer });
      if (response.data.status === 0) {
        return response.data.data;
      } else {
        throw new Error(response.data.message || '提交答案失败');
      }
    } catch (error) {
       const errorMessage = error.response?.data?.message || error.message;
       throw new Error(errorMessage);
    }
  }
};

export default quizGameService;